import Vue from 'vue';

const defaultPartData = {
  // 部分的基础信息
  form: {
    title: '',
    description: ''
  },
  // 部分的分数配置
  scores: {
    question_type_1: 1,
    question_type_2: 1,
    question_type_3: 1,
    question_type_3_miss: 0,
    question_type_4: 1,
    question_type_4_miss: 0,
    question_type_5: 1,
    question_type_8: 1
  },
  banks: []
}
const state = {
  parts: []
}

const mutations = {
  CREATE_PART: (state, data) => {
    if (!data) {
      const item = JSON.parse(JSON.stringify(defaultPartData))
      state.parts.push(item);
    } else {
      state.parts.push(data);
    }
  },
  REMOVE_PART: (state, index) => {
    state.parts.splice(index, 1);
  },
  INSERT_PART: (state, {
    index,
    data
  }) => {
    state.parts.splice(index, 0, data);
  },
  SET_PART_DATA: (state, {
    partIndex,
    data
  }) => {
    Vue.set(state.parts, partIndex, data);
  },
  CLEAR_PARTS: (state) => {
    state.parts = [];
    const item = JSON.parse(JSON.stringify(defaultPartData))
    Vue.set(state.parts, 0, item);
  },
  REMOVE_BANK: (state, {
    partIndex,
    bankId
  }) => {
    const data = state.parts[partIndex].banks;
    const index = data.findIndex(item => item.id == bankId)
    data.splice(index, 1);
    Vue.set(state.parts[partIndex], 'banks', data);
  },
  REMOVE_BANK_ALL: (state, {
    partIndex,
    bank
  }) => {
    const data = state.parts[partIndex].banks; // 原数据
    // 如果选中数据跟原数据某一条标识相等，删除对应的原数据
    bank.forEach((item) => {
      data.forEach((itemI, indexI) => {
        if (item.id === itemI.id) {
          data.splice(indexI, 1);
        }
      });
    });
  },
  INIT_PARTS: (state, data) => {
    state.parts = data;
  }
}

const actions = {
  // 添加部分
  addPart({
    commit
  }, data) {
    commit('CREATE_PART', data)
  },
  // 移除部分
  removePart({
    commit
  }, index) {
    commit('REMOVE_PART', index)
  },
  // 移动部分
  movePart({
    state,
    commit
  }, {
    oldIndex,
    newIndex
  }) {
    const data = state.parts[oldIndex];
    // 移除
    commit('REMOVE_PART', oldIndex)
    // 增加
    commit('INSERT_PART', {
      index: newIndex,
      data
    });
  },
  // 设置部分内容
  setPart({
    commit
  }, data) {
    commit('SET_PART_DATA', data)
  },
  // 清空部分
  clearParts({
    commit
  }) {
    commit('CLEAR_PARTS');
  },
  // 移除题库
  removeBank({
    commit
  }, data) {
    commit('REMOVE_BANK', data)
  },
  // 批量移除题库
  removeBankAll({
    commit
  }, data) {
    commit('REMOVE_BANK_ALL', data)
  },
  // 初始化数据
  initParts({
    commit
  }, data) {
    const parts = [];
    // 组装数据
    data.forEach(part => {
      const banks = [];
      if (part.value) {
        part.value.forEach(bank => {
          banks.push({
            ...bank.bank,
            topics: bank.topics
          })
        })
      }
      parts.push({
        // 部分的基础信息
        form: {
          title: part.title,
          description: part.description
        },
        // 部分的分数配置
        scores: part.scores == null ? defaultPartData.scores : part.scores,
        banks: banks
      })
    })
    // 更新数据
    commit('INIT_PARTS', parts);
  }
}

/** 累加器 **/
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
const getters = {
  // 部分数据
  parts: state => state.parts,
  // 试卷总题数
  topicsTotalCount: state => {
    const parts = state.parts;
    const arr = parts.map(part => {
      // 每个部分的总题数
      const total = part.banks.map(bank => {
        const topicCounts = [];
        // 每个题库的总题数
        Object.keys(bank.topics).forEach(type => {
          topicCounts.push(bank.topics[type]);
        })
        return sum(topicCounts);
      })
      // 当前部分所有题库的总题数
      return sum(total);
    });

    // 当前试卷的总题数
    return sum(arr);
  },
  // 题库部分的提交数据
  partsSubmitData: state => {
    const parts = state.parts;
    const data = [];
    parts.forEach((part, index) => {
      const banks = part.banks.map(bank => {
        return {
          scores: part.scores,
          bank: bank.id,
          topics: bank.topics
        }
      })
      data.push({
        number: index + 1,
        title: part.form.title,
        description: part.form.description,
        banks: banks,
        scores: part.scores
      })
    })

    return data;
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
